#include "../../include/direntop.h"
#include <dirent.h>

#define MODE (S_IRWXU | S_IRWXG | S_IRWXO)

int readconf(char *filename, char conf[][128])
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("fopen");
        return 0;
    }
    char buf[128];
    int i = 0;
    while ((fscanf(fp, "%s\n", buf)) > 0)
    {
        strcpy(conf[i], buf);
        i++;
    }
    if (i != T)
    {
        return -1;
    }
    fclose(fp);
    return i;
}

int myls(char *buf)
{
    bzero(buf, sizeof(buf));
    DIR *dirp = opendir(".");
    struct stat statbuf;
    struct dirent *pdirent;
    while ((pdirent = readdir(dirp)) != NULL)
    {
        char pbuf[512];
        int ret = stat(pdirent->d_name, &statbuf);
        // ERROR_CHECK(ret, -1, "stat");
        char tbuf[50];
        strcpy(tbuf, ctime(&statbuf.st_mtime));
        tbuf[strlen(tbuf) - 1] = 0;
        sprintf(pbuf, "%6o %ld %s %s %8ld %s %s\n",
                statbuf.st_mode,
                statbuf.st_nlink,
                getpwuid(statbuf.st_uid)->pw_name,
                getgrgid(statbuf.st_gid)->gr_name,
                statbuf.st_size,
                tbuf,
                pdirent->d_name);
        strcat(buf, pbuf);
    }
}

int myrmfile(char *filename)
{
    struct stat stat_buf;
    int ret = stat(filename, &stat_buf);
    if (ret == -1)
    {
        return -1;
    }
    struct dirent *dirp;
    char newpath[1024];

    if (__S_IFDIR & stat_buf.st_mode)
    {
        DIR *dp = opendir(filename);
        if (dp == NULL)
            return -1;

        while ((dirp = readdir(dp)) != NULL)
        {
            if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
            {
                continue;
            }
            sprintf(newpath, "%s%s%s", filename, "/", dirp->d_name);
            if (dirp->d_type == DT_DIR)
            {
                myrmfile(newpath);
            }
            else
            {
                unlink(newpath);
            }
        }
        closedir(dp);
        rmdir(filename);
    }
    else
    {
        unlink(filename);
    }
    return 0;
}

int mymkdir(event_t * evt, char *dirname)
{
    DIR *dir = NULL;
    if ((dir = opendir(dirname)) == NULL)
    {
        int ret = mkdir(dirname, MODE);
        if (ret != 0)
        {
            return -1;
        }
        file_t file;
        bzero(&file, sizeof(file));
        file.parent_id = evt->usrdata->pwdid;
        strcpy(file.filename, dirname);
        file.owner_id = evt->usrdata->id;
        file.fileSize = 4096;
        file.type = 'd';
        insert_file(evt, &file);        //将新文件插入数据库中

        printf("directory %s created success!\n", dirname);
    }
    else
    {
        printf("%s exist!\n", dirname);
        return -1;
    }
    return 0;
}

int mycd(event_t * evt, char * dirname) {
    char * pwd = evt->usrdata->pwd;
    if(strcmp(dirname, ".") == 0) {
        return 0;
    } else if(strcmp(dirname, "..") == 0) {
        int ret = fallback(pwd);
        if(ret == -1)
            return -1;
        file_t file;
        search_file(evt, &file, pwd);
        evt->usrdata->pwdid = file.id;
    } else {
        strcat (evt->usrdata->pwd, dirname);
        file_t file;
        search_file(evt, &file, pwd);
        evt->usrdata->pwdid = file.id;
    }
    return 0;
}
 
int fallback(char * path) {
    int i = strlen(path);
    while(i != 0 && path[i] != '/'){
        i--;
    }
    if(i == 0)
    {
        return -1;
    } else {
        path[i] = '\0';
    }
    return 0;
}

#if 0
int main(int argc, char *argv[]) {  //testmymkdir
    mymkdir(argv[1]);
    return 0;
}
#endif

#if 0
int main (int argc, char *argv[]) { //testls
    ARGS_CHECK(argc, 2);
    int ret = myrmfile(argv[1]);
    if(ret == -1) {
        puts("No such file or directory");
    }
    return 0;
}

#endif

#if 0
int main (int argc, char *argv[]) { //testcd
    ARGS_CHECK(argc, 2);
    
    DIR *dir = opendir(argv[1]);
    ERROR_CHECK(dir, NULL, "opendir");

    char buf[4096];
    int ret = myls(buf, dir);
    printf("%s", buf);
    
    bzero(buf, sizeof(buf));
    scanf("%s", buf);
    //printf("%s\n", buf);
    chdir(buf);
    // mycd(buf);
    // mypwd(buf);
    mycd(buf);
    getcwd(buf, sizeof(buf));
    printf("current path: %s\n", buf);
    //myls(buf, dir);
    printf("%s", buf);
    
    closedir(dir);
    return 0;
}

#endif

#if 0
int main (int argc, char *argv[]) { //testls
    ARGS_CHECK(argc, 2);
    
    DIR *dir = opendir(argv[1]);
    ERROR_CHECK(dir, NULL, "opendir");

    char buf[4096];
    int ret = myls(buf, dir);
    printf("%s", buf);
    
    return 0;
}
#endif
#if 0
int main (int argc, char *argv[]) { //testreadconf
    ARGS_CHECK(argc, 2);
    char conf[T][128];
    bzero(conf,sizeof(conf));
    int ret = readconf(argv[1], conf);
    ERROR_CHECK(ret , -1, "readconf");
    return 0;
}
#endif
